package com.atxiaodei.kabudasocketclientdemo;

import java.io.*;
import java.net.Socket;

/**
 * @ClassName SocketClientDemo
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2021/10/27 16:23
 * @Version 1.0
 **/
public class SocketClientDemo {

    public static void main(String[] args) {

        try {
            Socket socket = new Socket("localhost", 8080);
            // 客户端向服务端发送消息
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("我是客户端1，我现在发送一个消息\n");
            bufferedWriter.flush();

            // 客户端接收服务端消息
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = bufferedReader.readLine();// 读取服务端一行数据
            System.out.println("服务端返回的消息是："+line);

            bufferedWriter.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
